using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Skywave.Windows.Forms
{
    public partial class SheetEditorForm : Form
    {
        public SheetEditorForm()
        {
            InitializeComponent();
        }

        private Sheet mDataSheet;
        public Sheet DataSheet
        {
            get { return mDataSheet; }
            set { mDataSheet = value; }
        }

        private void SheetEditorForm_Load(object sender, EventArgs e)
        {
            if (mDataSheet == null)
            {
                this.DialogResult = DialogResult.Cancel;
                Close();
            }
            numericUpDownRow.Value = mDataSheet.PrintAreaEndCell.Y;
            numericUpDownColumn.Value = mDataSheet.PrintAreaEndCell.X;
            numericUpDownSelStartRow.Value = 1;
            numericUpDownSelStartColumn.Value = 1;
            numericUpDownSelEndRow.Value = 1;
            numericUpDownSelEndColumn.Value = 1;
            checkBoxRightToLeft.Checked = (mDataSheet.RightToLeft == RightToLeft.Yes);
            GetSelectionProperties();
        }

        private void pictureBoxPreviewSelected_Paint(object sender, PaintEventArgs e)
        {
            Point oldPrintAreaStart = mDataSheet.PrintAreaStartCell;
            Point oldPrintAreaEnd = mDataSheet.PrintAreaEndCell;

            //int oldPrintAreaRow = mDataSheet.PrintAreaRow;
            //int oldPrintAreaColumn = mDataSheet.PrintAreaColumn;
            mDataSheet.PrintAreaStartCell = new Point((int)numericUpDownSelStartColumn.Value, (int)numericUpDownSelStartRow.Value);
            mDataSheet.PrintAreaEndCell = new Point((int)numericUpDownSelEndColumn.Value, (int)numericUpDownSelEndRow.Value);
            mDataSheet.PrintOn(e.Graphics, pictureBoxPreviewSelected.DisplayRectangle, true, true, true);
            mDataSheet.PrintAreaStartCell = oldPrintAreaStart;
            mDataSheet.PrintAreaEndCell = oldPrintAreaEnd;
        }

        private void pictureBoxPreviewAll_Paint(object sender, PaintEventArgs e)
        {
            mDataSheet.PrintOn(e.Graphics, pictureBoxPreviewAll.DisplayRectangle, true, true, true);
        }

        private void numericUpDownRow_ValueChanged(object sender, EventArgs e)
        {
            mDataSheet.PrintAreaRow = (int)numericUpDownRow.Value;
            pictureBoxPreviewAll.Refresh();
        }

        private void numericUpDownColumn_ValueChanged(object sender, EventArgs e)
        {
            mDataSheet.PrintAreaColumn = (int)numericUpDownColumn.Value;
            pictureBoxPreviewAll.Refresh();
        }

        private void SelectedRangeChanged(object sender, EventArgs e)
        {
            pictureBoxPreviewSelected.Refresh();
            GetSelectionProperties();
        }

        private SheetRange GetSelRange()
        {
            int row1 = (int)numericUpDownSelStartRow.Value;
            int col1 = (int)numericUpDownSelStartColumn.Value;
            int row2 = (int)numericUpDownSelEndRow.Value;
            int col2 = (int)numericUpDownSelEndColumn.Value;
            return mDataSheet.Range(row1, col1, row2, col2);
        }
        private bool internalChange = false;
        private void GetSelectionProperties()
        {
            SheetRange selRange = GetSelRange();
            Color xColor;
            Font xFont;

            internalChange = true;
            xColor = selRange.BackColor;
            selBackColor.BackColor = (xColor == Color.Empty) ? mDataSheet.Defaults.BackColor : xColor;
            xColor = selRange.BorderColor;
            selBorderColor.BackColor = (xColor == Color.Empty) ? mDataSheet.Defaults.BorderColor : xColor;
            toolTipMain.SetToolTip(selBorderWidth_All, selRange.GetBorder(SheetRangeBorderTypes.InnerOuter).ToString());
            toolTipMain.SetToolTip(selBorderWidth_Bottom, selRange.GetBorder(SheetRangeBorderTypes.Bottom).ToString());
            toolTipMain.SetToolTip(selBorderWidth_Inner, selRange.GetBorder(SheetRangeBorderTypes.Inner).ToString());
            toolTipMain.SetToolTip(selBorderWidth_Left, selRange.GetBorder(SheetRangeBorderTypes.Left).ToString());
            toolTipMain.SetToolTip(selBorderWidth_MiddleHorizontal, selRange.GetBorder(SheetRangeBorderTypes.MiddleHorizontal).ToString());
            toolTipMain.SetToolTip(selBorderWidth_MiddleVertical, selRange.GetBorder(SheetRangeBorderTypes.MiddleVertical).ToString());
            toolTipMain.SetToolTip(selBorderWidth_Outer, selRange.GetBorder(SheetRangeBorderTypes.Outer).ToString());
            toolTipMain.SetToolTip(selBorderWidth_Right, selRange.GetBorder(SheetRangeBorderTypes.Right).ToString());
            toolTipMain.SetToolTip(selBorderWidth_Top, selRange.GetBorder(SheetRangeBorderTypes.Top).ToString());
            xColor = selRange.ForeColor;
            selForeColor.BackColor = (xColor == Color.Empty) ? mDataSheet.Defaults.ForeColor : xColor;
            xFont = selRange.Font;
            xFont = (xFont == null) ? mDataSheet.Defaults.Font : xFont;
            selFont.Text = xFont.FontFamily.Name.ToString() + ", " + xFont.Size.ToString() + ", " + xFont.Style.ToString();
            selMerged.Checked = selRange.Merged;
            selRightToLeft.Checked = selRange.RightToLeft;
            selShrinkToFit.Checked = selRange.ShrinkToFit;
            selText.Text = (selRange.Text == null) ? "" : selRange.Text.ToString();
            RefreshTextAlign();
            selVerticalText.Checked = selRange.VerticalText;
            selWrap.Checked = selRange.Warp;
            int x;
            x = mDataSheet.Columns[(int)numericUpDownSelStartColumn.Value].Width;
            selColumnsWidth.Value = (x == -1) ? mDataSheet.Defaults.ColumnWidth : x;
            x = mDataSheet.Rows[(int)numericUpDownSelStartRow.Value].Height;
            selRowsHeight.Value = (x == -1) ? mDataSheet.Defaults.RowHeight : x;
            internalChange = false;
        }

        private void RefreshTextAlign()
        {
            SheetRange selRange = GetSelRange();
            selTextAlign_BottomCenter.ImageList = (selRange.TextAlign == ContentAlignment.BottomCenter) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_BottomLeft.ImageList = (selRange.TextAlign == ContentAlignment.BottomLeft) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_BottomRight.ImageList = (selRange.TextAlign == ContentAlignment.BottomRight) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_MiddleCenter.ImageList = (selRange.TextAlign == ContentAlignment.MiddleCenter) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_MiddleLeft.ImageList = (selRange.TextAlign == ContentAlignment.MiddleLeft) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_MiddleRight.ImageList = (selRange.TextAlign == ContentAlignment.MiddleRight) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_TopCenter.ImageList = (selRange.TextAlign == ContentAlignment.TopCenter) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_TopLeft.ImageList = (selRange.TextAlign == ContentAlignment.TopLeft) ? imageListTextAlignSelected : imageListTextAlign;
            selTextAlign_TopRight.ImageList = (selRange.TextAlign == ContentAlignment.TopRight) ? imageListTextAlignSelected : imageListTextAlign;
        }

        private void selText_TextChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().Text = selText.Text;
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selForeColor_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            colorDialog1.Color = selForeColor.BackColor;
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                GetSelRange().ForeColor = colorDialog1.Color;
                selForeColor.BackColor = colorDialog1.Color;
                pictureBoxPreviewSelected.Refresh();
                pictureBoxPreviewAll.Refresh();
            }
        }

        private void selBackColor_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            colorDialog1.Color = selBackColor.BackColor;
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                GetSelRange().BackColor = colorDialog1.Color;
                selBackColor.BackColor = colorDialog1.Color;
                pictureBoxPreviewSelected.Refresh();
                pictureBoxPreviewAll.Refresh();
            }
        }

        private void selBorderColor_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            colorDialog1.Color = selBorderColor.BackColor;
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                GetSelRange().BorderColor = colorDialog1.Color;
                selBorderColor.BackColor = colorDialog1.Color;
                pictureBoxPreviewSelected.Refresh();
                pictureBoxPreviewAll.Refresh();
            }
        }

        private void selFont_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            fontDialog1.Font = selFont.Font;
            if (fontDialog1.ShowDialog(this) == DialogResult.OK)
            {
                GetSelRange().Font = fontDialog1.Font;
                Font xFont = fontDialog1.Font;
                selFont.Text = xFont.FontFamily.Name.ToString() + ", " + xFont.Size.ToString() + ", " + xFont.Style.ToString();
                pictureBoxPreviewSelected.Refresh();
                pictureBoxPreviewAll.Refresh();
            }
        }

        private void selBorderWidth_Outer_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.Outer, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_Top_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.Top, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_All_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.InnerOuter, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_Right_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.Right, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_Inner_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.Inner, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_Left_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.Left, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_MiddleHorizontal_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.MiddleHorizontal, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_Bottom_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.Bottom, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selBorderWidth_MiddleVertical_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            int borderWidth = (int)selBorderWidth.Value;
            GetSelRange().SetBorder(SheetRangeBorderTypes.MiddleVertical, borderWidth);
            toolTipMain.SetToolTip((Control)sender, borderWidth.ToString());
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_TopRight_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.TopRight;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_TopCenter_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.TopCenter;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_TopLeft_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.TopLeft;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_MiddleRight_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.MiddleRight;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_MiddleCenter_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.MiddleCenter;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_MiddleLeft_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.MiddleLeft;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_BottomRight_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.BottomRight;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_BottomCenter_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.BottomCenter;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selTextAlign_BottomLeft_Click(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().TextAlign = ContentAlignment.BottomLeft;
            RefreshTextAlign();
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selMerged_CheckedChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().Merged = ((CheckBox)sender).Checked;
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selRightToLeft_CheckedChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().RightToLeft = ((CheckBox)sender).Checked;
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selShrinkToFit_CheckedChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().ShrinkToFit = ((CheckBox)sender).Checked;
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selVerticalText_CheckedChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().VerticalText = ((CheckBox)sender).Checked;
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selWrap_CheckedChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            GetSelRange().Warp = ((CheckBox)sender).Checked;
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selColumnsWidth_ValueChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            int x1 = (int)numericUpDownSelStartColumn.Value;
            int x2 = (int)numericUpDownSelEndColumn.Value;
            for (int c = x1; c <= x2; c++)
            {
                mDataSheet.Columns[c].Width = (int)selColumnsWidth.Value;
            }
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void selRowsHeight_ValueChanged(object sender, EventArgs e)
        {
            if (internalChange) return;
            int x1 = (int)numericUpDownSelStartRow.Value;
            int x2 = (int)numericUpDownSelEndRow.Value;
            for (int r = x1; r <= x2; r++)
            {
                mDataSheet.Rows[r].Height = (int)selRowsHeight.Value;
            }
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }

        private void OK_Button_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void checkBoxRightToLeft_CheckedChanged(object sender, EventArgs e)
        {
            mDataSheet.RightToLeft = (checkBoxRightToLeft.Checked) ? RightToLeft.Yes : RightToLeft.No;
            pictureBoxPreviewSelected.Refresh();
            pictureBoxPreviewAll.Refresh();
        }
    }
}